/**
 * 
 */
package com.android.dwt.util;

import java.lang.reflect.Field;
import java.util.Calendar;

import android.app.AlertDialog;

/**
 * @author xiang
 *
 */
public class Utils {
	
	public static int getDaysBetween (Calendar d1, Calendar d2) {  
	    if (d1.after(d2)) {  // swap dates so that d1 is start and d2 is end  
	        Calendar swap = d1;  
	        d1 = d2;  
	        d2 = swap;  
	    }

		int days = d2.get(Calendar.DAY_OF_YEAR)-d1.get(Calendar.DAY_OF_YEAR);
		int y2 = d2.get(Calendar.YEAR);
		
		if (d1.get(Calendar.YEAR) != y2) {
			d1 = (Calendar) d1.clone();
			do {
				days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
				d1.add(Calendar.YEAR, 1);
			} while (d1.get(Calendar.YEAR) != y2);
		}
	    
	    return days;  
	}
	
	public static int getWeeks(Calendar w1, Calendar w2) {
		
		if (w1.after(w2)) { // swap dates so that d1 is start and d2 is end
			Calendar swap = w1;
			w1 = w2;
			w2 = swap;
		}
		
		int weeks = getDaysBetween(w1, w2) / 7;
		
		return weeks;
	}
	
	public static int getMonths(Calendar m1, Calendar m2) {
		
		if (m1.after(m2)) { // swap dates so that d1 is start and d2 is end
			Calendar swap = m1;
			m1 = m2;
			m2 = swap;
		}
		
		int months = 0;
		m1.add(Calendar.MONTH, 1);
		
		while (m1.before(m2)) {
			months++;
			m1.add(Calendar.MONTH, 1);
		}
		
		return months;
	}
	
	public static int getYears(Calendar y1, Calendar y2) {
		
		if (y1.after(y2)) {
			Calendar swap = y1;
			y1 = y2;
			y2 = swap;
		}
		
		int years = 0;
		y1.add(Calendar.YEAR, 1);
		
		while (y1.before(y2)) {
			years++;
			y1.add(Calendar.YEAR, 1);
		}
		
		return years;
	}
	
	public static int getWorkingDays(Calendar d1, Calendar d2) {
		int result = -1;
		
		if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end
			Calendar swap = d1;
			d1 = d2;
			d2 = swap;
		}

		int charge_start_date = 0;	// 开始日期的日期偏移量
		int charge_end_date = 0;	// 结束日期的日期偏移量

		int stmp = 7 - d1.get(Calendar.DAY_OF_WEEK);
		int etmp = 7 - d2.get(Calendar.DAY_OF_WEEK);
		if (stmp != 0 && stmp != 6) {	// 开始日期为星期六和星期日时偏移量为0
			charge_start_date = stmp - 1;
		}
		if (etmp != 0 && etmp != 6) {	// 结束日期为星期六和星期日时偏移量为0
			charge_end_date = etmp - 1;
		}
		
		result = (getDaysBetween(getNextMonday(d1), getNextMonday(d2)) / 7)
				* 5 + charge_start_date - charge_end_date;
		
		return result;
	}
	
	public static Calendar getNextMonday(Calendar date) {
		Calendar result = null;
		result = date;
		do {
			result = (Calendar) result.clone();
			result.add(Calendar.DATE, 1);
		} while (result.get(Calendar.DAY_OF_WEEK) != 2);
		return result;
	}
	
	public static void closeAlertDialog(AlertDialog ad, boolean showing) {
		
		Field field = null;
		try {
			
			field = ad.getClass().getSuperclass().getDeclaredField("mShowing");
			field.setAccessible(true);
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
		try {
			
			field.set(ad, showing);
			
		} catch (IllegalArgumentException e) {
			
			e.printStackTrace();
			
		} catch (IllegalAccessException e) {
			
			e.printStackTrace();
		}
		
		return;
	}
	
}
